﻿using System;

namespace DotNetImage.Imaging.Filters
{
    /// <summary>
    /// This one works by summing up values in top line and subtract the sum of the bottom line.
    /// Calculation is made per color.
    /// ooooooo
    ///    x
    /// ooooooo
    /// </summary>
    public class EdgeDetectHorizontalFilter : Conv3x3
    {
        [ParameterAttribute(Name = "Edge Detection Type", DefaultValue = (int)EdgeDetectionType.EDGE_DETECT_HORZ)]
        public EdgeDetectionType DetectionType { get { return EdgeDetectionType.EDGE_DETECT_HORZ; } }

        protected override void PrepareData(DotNetImage bmp)
        {
            base.PrepareData(bmp);
            bmp.CreatePixelCopy();
            startX = 3; // Skip 3 left and right pixels
            startY = 1;
        }

        protected override void ProcessPixel(ref Pixel pix, uint x = 0, uint y = 0)
        {
            Pixel pixTopLine = Subject.GetPreservedPixel(x - 3, y - 1)
                + Subject.GetPreservedPixel(x - 2, y - 1)
                + Subject.GetPreservedPixel(x - 1, y - 1)
                + Subject.GetPreservedPixel(x - 0, y - 1)
                + Subject.GetPreservedPixel(x + 1, y - 1)
                + Subject.GetPreservedPixel(x + 2, y - 1)
                + Subject.GetPreservedPixel(x + 3, y - 1);

            Pixel pixBottomLine = Subject.GetPreservedPixel(x - 3, y + 1)
                + Subject.GetPreservedPixel(x - 2, y + 1)
                + Subject.GetPreservedPixel(x - 1, y + 1)
                + Subject.GetPreservedPixel(x - 0, y + 1)
                + Subject.GetPreservedPixel(x + 1, y + 1)
                + Subject.GetPreservedPixel(x + 2, y + 1)
                + Subject.GetPreservedPixel(x + 3, y + 1);

            pixTopLine.SubtractAbs(pixBottomLine);
            pix = pixTopLine;
        }
    }
}
